package com.yunqiao.api.controller;

import java.util.Map;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import springfox.documentation.annotations.ApiIgnore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.yunqiao.api.annotation.Login;
import com.yunqiao.common.exception.CustomApiException;
import com.yunqiao.common.validator.ValidatorUtils;
import com.yunqiao.common.utils.ApiResponse;
import com.yunqiao.service.api.entity.EvaluateEntity;
import com.yunqiao.service.api.EvaluateService;
import com.yunqiao.common.utils.PageUtils;
import com.yunqiao.common.utils.R;


/**
 * 评价
 * @author Crocutax
 * @email wangxw725@163.com
 * @date 2019-03-03 21:58:45
 */
@RestController
@RequestMapping("api/evaluate")
@Api(tags = "EvaluateApiController" ,description = "评价管理")
public class EvaluateApiController {

    @Autowired
    private EvaluateService evaluateService;

    /**
     * 列表
     */
    @Login
    @ApiImplicitParams({
        @ApiImplicitParam(name = "targetUserId", value = "被评价人id", required = true ,paramType = "query" , dataType = "int"),
        @ApiImplicitParam(name = "page", value = "页码", required = false ,defaultValue = "1",paramType = "query" , dataType = "int"),
        @ApiImplicitParam(name = "limit", value = "数量", required = false,defaultValue = "10",paramType = "query" , dataType = "int")
    })
    @GetMapping("/list")
    @ApiOperation("查询评价 列表")
    public ApiResponse<PageUtils<EvaluateEntity>> list(@ApiIgnore @RequestParam Map<String, Object> params){
        PageUtils page = evaluateService.queryPage(params);
        return new ApiResponse<>(page);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation("查询单个评价")
    public ApiResponse<EvaluateEntity> info(@PathVariable("id") Long id){
        if (id == null) {
            throw new CustomApiException("ID不能为空");
        }        EvaluateEntity evaluate = evaluateService.getById(id);
        return new ApiResponse<>(evaluate);
    }

    /**
     * 保存 添加评价
     */
    @Login
    @PostMapping("/save")
    @ApiOperation("保存评价")
    public ApiResponse save(@RequestBody EvaluateEntity evaluate){
        ValidatorUtils.validateEntity(evaluate);
        evaluateService.addEvaluate(evaluate);
        return new ApiResponse<>();
    }

    /**
     * 修改
     */
    @Login
    @PutMapping("/update/{id}")
    @ApiOperation("修改评价")
    public ApiResponse update(@PathVariable Long id ,@RequestBody EvaluateEntity evaluate){
        EvaluateEntity dbItem = evaluateService.getById(id);
        if(dbItem==null){
            throw new CustomApiException("ID有误, 未找到对应数据");
        }
        evaluateService.updateById(evaluate);
        return new ApiResponse<>();
    }

    /**
     * 删除
     */
    @Login
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除评价")
    public ApiResponse delete(@PathVariable Long id){
        if (id == null) {
            throw new CustomApiException("ID不能为空");
        }
        evaluateService.removeById(id);
        return new ApiResponse<>();
    }


    /**
     * 删除评价 修改为删除状态
     */
    @Login
    @DeleteMapping("/deleteState/{id}")
    @ApiOperation("删除评价 修改为删除状态")
    public ApiResponse deleteState(@PathVariable Long id){
        if (id == null) {
            throw new CustomApiException("ID不能为空");
        }
        evaluateService.deleteState(id);
        return new ApiResponse<>();
    }
    /**
     * 获取评价平均分
     */
    @ApiImplicitParam(name = "targetUserId", value = "被评价人id", required = true ,paramType = "query" , dataType = "int")
    @Login
    @GetMapping("/getScore")
    @ApiOperation("获取评价平均分")
    public ApiResponse<Double> getScore(Integer targetUserId){
        if (targetUserId == null) {
            throw new CustomApiException("ID不能为空");
        }
        Double score = evaluateService.getScore(targetUserId);
        return new ApiResponse<>(score);
    }

    /**
     * 获取评价
     */
    @ApiImplicitParams({
        @ApiImplicitParam(name = "orderId", value = "订单id", required = true ,paramType = "query" , dataType = "long"),
        @ApiImplicitParam(name = "targetUserId", value = "被评价人id", required = true ,paramType = "query" , dataType = "long")
    })
    @Login
    @GetMapping("/getEvaluate")
    @ApiOperation("获取评价")
    public ApiResponse<EvaluateEntity> getEvaluate(Long orderId, Long targetUserId){
        if (orderId == null) {
            throw new CustomApiException("订单ID不能为空");
        }
        if (targetUserId == null) {
            throw new CustomApiException("用户ID不能为空");
        }
        EvaluateEntity evaluateEntity = evaluateService.getEvaluate(orderId, targetUserId);
        return new ApiResponse<>(evaluateEntity);
    }



}
